package DesignPattern;

import java.util.ArrayList;
import java.util.List;

public abstract class Observer {
    public abstract void update();
    protected Subject subject;
}

class Subject {
    private List<Observer> observers = new ArrayList<>();
    public int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }

    public void attach(Observer observer){
        observers.add(observer);
    }
    public void notifyAllObservers(){
         for (Observer observer : observers) {
             observer.update();
         }
     }
}

class BinaryObserver extends Observer{
    public BinaryObserver(Subject subject){
        this.subject = subject;
        this.subject.attach(this);
    }
    @Override
    public void update() {
        System.out.println("Binary String:"+ Integer.toBinaryString(subject.getState()));
    }
}

class OctalObserver extends Observer{
    public OctalObserver(Subject subject){
        this.subject = subject;
        this.subject.attach(this);
    }

    @Override
    public void update() {
        System.out.println("Octal String:"+ Integer.toOctalString(subject.getState()));
    }
}

class test{
    public static void main(String[] args) {
        Subject subject = new Subject();

        new OctalObserver(subject);
        new BinaryObserver(subject);

        System.out.println("First state change: 15");
        subject.setState(15);
        System.out.println("Second state change: 10");
        subject.setState(10);

        int[] a = new int[]{1,2,3,4,5};
        int[] b = new int[5];
        b = a;
        a[1] = 0;
        for (int i : a) {
            System.out.println(i);
        }

        for (int i : b) {
            System.out.println(i);
        }

    }

}
